package com.wj.springboot2demo.multids;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;

@Configuration
@MapperScan(basePackages = { "com.wj.springboot2demo.domain.searchmanager" }, sqlSessionTemplateRef = "searchManagerSqlSessionTemplate")
@ConfigurationProperties(prefix = "spring.datasource.druid")
public class SearchManagerDsConfiger extends AbstractDataSourceConfiger {
    
    @Bean(name = "searchManagerDataSource")
    @Primary
    // 必须加此注解，不然报错，下一个类则不需要添加
    // prefix值必须是application.properteis中对应属性的前缀
    public DataSource userDataSource() {
         DataSource dataSource = DruidDataSourceBuilder.create().build();
         return dataSource;
    }

    @Bean
    public SqlSessionFactory searchManagerSqlSessionFactory(@Qualifier("searchManagerDataSource") DataSource dataSource) throws Exception {

        return createSessionFactory(dataSource);
    }

    @Bean
    public SqlSessionTemplate searchManagerSqlSessionTemplate(
            @Qualifier("searchManagerSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory); // 使用上面配置的Factory
        return template;
    }

}
